<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>测试openid</title>
    <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        var basePath="http://${header['host']}${pageContext.request.contextPath}";

        var resultData = '${resultData}';
        var code = '${resultData.code}';
        var access_token = '${resultData.data.accessToken}';
        var openId = '${resultData.data.openId}';
        var errorMsg = '${resultData.msg}';

        alert("openId:"+openId);
        alert("access_token:"+access_token);
    </script>
</head>
<body>

openid:${resultData.data.openId}
access_token:${resultData.data.accessToken}

    订单号<input name="tradeId" id="tradeId" value="20"/>
    支付方式<select name="payType" id="payType">
    <option value="0">支付宝</option>
    <option value="1" selected>微信公众号</option>
</select>
    openid<input name="access_token" id="access_token" value="${resultData.data.accessToken}"/>
<button onclick="onBridgeReady()">继续支付</button>
<script>
    if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        }else if (document.attachEvent){
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    }

    function onBridgeReady(){
        $.ajax({
            type:"POST",
            url:"http://b.yangcangclub.com/yangcangclub/v1/trade/api/continuePay",
            data:{
                tradeId:$("#tradeId").val(),
                payType:$("#payType").val(),
                access_token:$("#access_token").val()
            },
            dataType:"json",
            success:function(data){
                var obj = data.data.tradePayParam;
                WeixinJSBridge.invoke(
                 'getBrandWCPayRequest', {
                 "appId" : obj.appId,     //公众号名称，由商户传入
                 "timeStamp":obj.timeStamp,         //时间戳，自1970年以来的秒数
                 "nonceStr" : obj.nonceStr, //随机串
                 "package" : obj.package,
                 "signType" : obj.signType,         //微信签名方式：
                 "paySign" : obj.paySign //微信签名
                 },
                 function(res){
                 if(res.err_msg == "get_brand_wcpay_request：ok" ) {}     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                 }
                 );
            }
        });

    }

</script>
</body>
</html>